#include "studetinformation.h"
#include "ui_studetinformation.h"
#include "QMessageBox"
StudetInformation::StudetInformation(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::StudetInformation),
    add_dialog(nullptr),
    mod_dialog(nullptr)
{
    ui->setupUi(this);

    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

    QVector<QStringList> stringlist = fileWriteAndRead().readAndParseFile("student.txt");
//    qDebug()<<stringlist;
    for(int i = 0; i < stringlist.size();i++)
    {
        Student stu(stringlist[i]);
        this->student.insert(stu.stu_number,stu);
    }
    for(int i = 0; i < stringlist.size();i++)
    {
        Student stu(stringlist[i]);
        this->student.insert(stu.stu_number,stu);
    }
//    qDebug()<<student.size();
    for(int i = 0; i < stringlist[0].size();i++)
    {
        ui->tableWidget->insertColumn(i);
    }

    printfTable();
}

StudetInformation::~StudetInformation()
{

    delete ui;
}

void StudetInformation::printfTable()
{
    while(ui->tableWidget->rowCount()>0)
    {
        ui->tableWidget->removeRow(ui->tableWidget->rowCount()-1);
    }

    for(int i = 0; i < student.size();i++)
    {
        ui->tableWidget->insertRow(i);
    }

    ui->tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("学号"));
    ui->tableWidget->setHorizontalHeaderItem(1, new QTableWidgetItem("姓名"));
    ui->tableWidget->setHorizontalHeaderItem(2, new QTableWidgetItem("专业"));
    ui->tableWidget->setHorizontalHeaderItem(3, new QTableWidgetItem("班级"));
    ui->tableWidget->setHorizontalHeaderItem(4, new QTableWidgetItem("已借图书数量"));
    int i = 0;
    for(auto j = student.begin(); j!=student.end();j++,i++)
    {
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(j.value().stu_number));
        ui->tableWidget->setItem(i, 1, new QTableWidgetItem(j.value().stu_name));
        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(j.value().stu_major));
        ui->tableWidget->setItem(i, 3, new QTableWidgetItem(j.value().stu_class));
        ui->tableWidget->setItem(i, 4, new QTableWidgetItem(QString::number(j.value().stu_bookNum)));
    }
}

void StudetInformation::closeEvent(QCloseEvent *event)
{
    this->close();
    emit is_close();
}

void StudetInformation::on_pushButton_save_clicked()
{
    QVector<QStringList> cnt;
    for(Student& stu:this->student)
    {
        cnt.push_back(stu.getStudent());
    }
//    qDebug()<<cnt;
    fileWriteAndRead().writeDataToFile("student.txt",cnt);
}

void StudetInformation::on_pushButton_close_clicked()
{
    this->close();
}

void StudetInformation::on_pushButton_add_clicked()
{
    if(add_dialog == nullptr)
    {
        add_dialog = new stu_add_Dialog(this);
    }
//    qDebug()<<add_dialog->flag;
    add_dialog->exec();
//    add_dialog->show();
//    qDebug()<<add_dialog->flag;
    if(add_dialog->flag == true)
    {
        QMessageBox* message = new QMessageBox(this);

        if(student.find(add_dialog->student[0]) == student.end())
        {
            student.insert(add_dialog->student[0],Student(add_dialog->student));
            message->setText("添加学生成功!");
        }
        else
        {
            message->setText("插入失败!学生学号重复,请使用修改功能!");
        }
        message->exec();
    }
    printfTable();
    delete  add_dialog;
    add_dialog=nullptr;
}

void StudetInformation::on_pushButton_mod_clicked()
{
    if(mod_dialog == nullptr)
    {
        mod_dialog = new stu_mod_Dialog(this);
    }
//    qDebug()<<add_dialog->flag;

//    add_dialog->show();
//    qDebug()<<add_dialog->flag;
    int row = ui->tableWidget->currentRow();
//    qDebug()<<row;
    if(row != -1)
    {
//        qDebug()<<ui->tableWidget->item(row,0)->text();
        mod_dialog->student=student[ui->tableWidget->item(row,0)->text()].getStudent();
        QStringList cnt = mod_dialog->student;
//        qDebug()<<mod_dialog->student;
        mod_dialog->printfData();
        mod_dialog->exec();
        if(mod_dialog->flag == true)
        {

            QMessageBox* message = new QMessageBox(this);
            if(cnt[0] != mod_dialog->student[0])
            {
                if(student.find(mod_dialog->student[0]) == student.end())
                {
                    student.remove(cnt[0]);
                    student.insert(mod_dialog->student[0],Student(mod_dialog->student));
                    message->setText("修改学生成功!");
                }
                else
                {
                    message->setText("修改失败!学生学号重复,先修改重复项!");
                }

            }
            else
            {
                student.insert(mod_dialog->student[0],Student(mod_dialog->student));
                 message->setText("修改学生成功!");
            }
             message->exec();

        }
    }

    printfTable();
    delete  mod_dialog;
    mod_dialog=nullptr;
}

void StudetInformation::on_pushButton_delet_clicked()
{
    int row = ui->tableWidget->currentRow();
//    qDebug()<<row;
    if(row != -1)
    {
//        qDebug()<<ui->tableWidget->item(row,0)->text();
        student.remove(ui->tableWidget->item(row,0)->text());

        ui->tableWidget->removeRow(row);
    }

    printfTable();
}
